package cn.com.sqlite.demo.common.aspect;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;

import javax.activation.DataSource;

import org.springframework.aop.AfterReturningAdvice;
import org.springframework.aop.MethodBeforeAdvice;

import cn.com.sqlite.demo.common.db.DataSourceContextHolder;

public class DataSourceAspect implements MethodBeforeAdvice,AfterReturningAdvice {

	public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
		 DataSourceContextHolder.clearDataSourceType(); 
	}

	public void before(Method method, Object[] args, Object target) throws Throwable {
		if(method.isAnnotationPresent((Class<? extends Annotation>) DataSource.class))
		{
			DataSource datasource = (DataSource) method.getAnnotation((Class<? extends Annotation>)DataSource.class) ;
			DataSourceContextHolder.setDataSourceType(datasource.getName());
		}else{
			
		}
	}

}
